package com.sunday.pinxinghui.model.login;

import android.accounts.Account;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.pinxinghui.MainActivity;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.base.Constant;
import com.sunday.pinxinghui.entity.Member;
import com.sunday.pinxinghui.model.WebViewActivity;
import com.sunday.pinxinghui.network.ApiClient;
import com.sunday.pinxinghui.network.ApiService;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by 刘涛 on 2017/8/14.
 */

public class RegisterActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.et_phone)
    ClearEditText etPhone;
    @Bind(R.id.et_code)
    ClearEditText etCode;
    @Bind(R.id.send_code)
    TextView sendCode;
    @Bind(R.id.et_pwd)
    ClearEditText etPwd;
    @Bind(R.id.protocol_check)
    ImageButton protocolCheck;
    @Bind(R.id.btn_register)
    TextView btnRegister;



     private TimeCount timeCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        titleView.setText("注册");
        timeCount=new TimeCount(60000,1000,sendCode);


    }
    private boolean isAgree = false;
    @OnClick({R.id.send_code,R.id.btn_register,R.id.user_protcol,R.id.protocol_check,R.id.member_protcol})
    void onClick(View v){
        switch (v.getId()){
            case R.id.send_code:
                String mobile=etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(mobile)|| !StringUtils.isMobileNO(mobile)){
                    ToastUtils.showToast(mContext,"请输入正确的手机号");
                    return;
                }
                sendCode(mobile);
                break;
            case R.id.btn_register:
                String mobile1=etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(mobile1)|| !StringUtils.isMobileNO(mobile1)){
                    ToastUtils.showToast(mContext,"请输入正确的手机号");
                    return;
                }
                String code=etCode.getText().toString().trim();
                if (TextUtils.isEmpty(code)){
                    ToastUtils.showToast(mContext,"请输入验证码");
                    return;
                }
                String pwd=etPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)){
                    ToastUtils.showToast(mContext,"请输入密码");
                    return;
                }
                if (pwd.length()<6||pwd.length()>10){
                    ToastUtils.showToast(mContext,"请输入6-10位的密码");
                    return;
                }


                if (!isAgree){
                    ToastUtils.showToast(mContext,"请阅读并同意用户协议");
                    return;
                }
               Call<ResultDO<Member>> call=ApiClient.getApiAdapter().register(mobile1,code,MD5Utils.MD5(pwd));
                call.enqueue(new Callback<ResultDO<Member>>() {
                    @Override
                    public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                        if (isFinish||response.body()==null){return;}
                        ResultDO<Member> resultDO = response.body();
                        if (response.body().getCode()==0){
                            ToastUtils.showToast(mContext, "注册成功！");
                            SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                            SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
                            Member member=resultDO.getResult();
                            BaseApplication.getInstance().setMember(member);
                            intent = new Intent(mContext, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            ToastUtils.showToast(mContext, resultDO.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                        ToastUtils.showToast(mContext, R.string.network_error);
                    }
                });

                break;
            case R.id.user_protcol:
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", ApiClient.REGISTER_URL);
                intent.putExtra("title","注册协议");
                startActivity(intent);
                break;
            case R.id.member_protcol:
                //会员协议
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", ApiClient.MEMBER_URL);
                intent.putExtra("title","会员协议");
                startActivity(intent);
                break;
            case R.id.protocol_check:
                if (!isAgree) {
                    protocolCheck.setSelected(true);
                } else {
                    protocolCheck.setSelected(false);
                }
                isAgree = !isAgree;
                break;
        }
    }

    private void sendCode(String mobile){
        Call<ResultDO> call= ApiClient.getApiAdapter().sendCode(null,mobile, Constant.CODE_ONE);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    ToastUtils.showToast(mContext,"验证码已发送到手机");
                    timeCount.start();
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount!=null){
            timeCount.cancel();
        }
    }
}
